package com.nowcoder.community.service;

import com.nowcoder.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

@Service
public class LikeService {

    @Autowired
    private RedisTemplate redisTemplate;

//    点赞
    public void like(int userId, int entityType, int entityId, int entityUserId) {
//        String entityLikekey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
//        boolean isMember = redisTemplate.opsForSet().isMember(entityLikekey, userId);
//        if(isMember){
//            redisTemplate.opsForSet().remove(entityLikekey, userId);
//        }else{
//            redisTemplate.opsForSet().add(entityLikekey, userId);
//        }
        redisTemplate.execute(new SessionCallback() {
           @Override
           public Object execute(RedisOperations redisOperations) throws DataAccessException {
               String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
               String userLikeKey = RedisKeyUtil.getUserLikeKey(entityUserId);

               boolean isMember = redisOperations.opsForSet().isMember(entityLikeKey, userId);

//               开启事务
               redisOperations.multi();

               if(isMember){
                   redisOperations.opsForSet().remove(entityLikeKey, userId);
                   redisOperations.opsForValue().decrement(userLikeKey);
               }else{
                   redisOperations.opsForSet().add(entityLikeKey, userId);
                   redisOperations.opsForValue().increment(userLikeKey);
               }
//                提交事务
               return redisOperations.exec();
           }
        });

    }

//    查询实体点赞的数量
    public long findEntityLikeCount(int entityType, int entityId) {
        String entityLikekey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        return redisTemplate.opsForSet().size(entityLikekey);
    }

//    查询某人对某实体的点赞状态
    public int findEntityLikeStatus(int userId, int entityType, int entityId) {
        String entityLikekey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        return redisTemplate.opsForSet().isMember(entityLikekey, userId) ? 1 : 0;
    }

//    查询某个用户获得赞的数量
    public int findUserLikeCount(int userId) {
        String userLikeKey = RedisKeyUtil.getUserLikeKey(userId);
        Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey);
        return count == null ? 0 : count.intValue();
    }
}
